import 'dart:async';
import 'dart:convert'; //模型转换需要用到这个类
import 'package:http/http.dart' as http;
import 'package:imitatectripapp/model/search_model.dart';

const SEARCH_URL =
    'https://m.ctrip.com/restapi/h5api/globalsearch/search?source=mobileweb&action=mobileweb&keyword=';

// 搜索接口

class SearchDao {
  static Future<SearchModel> searchKeyWord(String keyWord, String value) async {
    final response = await http.get(SEARCH_URL + '$keyWord');
    if (response.statusCode == 200) {
      // 解决中文乱码
      Utf8Decoder utf8decoder = Utf8Decoder();
      var result = json.decode(utf8decoder.convert(response.bodyBytes));
      // 只有当前输入的内容和服务端返回的内容一致时才渲染
      SearchModel model = SearchModel.fromJson(result);
      model.keyword = value;
      return model;
    } else {
      print('请求接口失败');
      throw Exception('请求接口失败');
    }
  }
}
